﻿package Gameplay.Visualizer
{	
	import flash.display.Sprite;	
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;	

	public class SoundVisualizer extends Sprite
	{
		private var FVisualizations:Vector.<AbstractVisualizer>;
		private var FCurrentVisualization:AbstractVisualizer;
		
		public function SoundVisualizer():void
		{	
			FVisualizations = new Vector.<AbstractVisualizer>;
			addEventListener(Event.ADDED_TO_STAGE, OnAddedToStage);			
		}
		
		private function OnAddedToStage(e:Event):void
		{
			for(var i:int = 0; i < numChildren; i++)
			{
				FVisualizations.push(AbstractVisualizer(getChildAt(i)));
				trace('Detected visualization: ' + getChildAt(i));
			}
			
			Visualization = 0;
		}
		
		public function Play():void
		{
			if(FCurrentVisualization)
				FCurrentVisualization.Play();
		}
		
		public function Stop():void
		{
			if(FCurrentVisualization)
				FCurrentVisualization.Stop();
		}
		
		public function set Sound(ASound:Sound):void
		{
			if(FCurrentVisualization)
				FCurrentVisualization.Sound = ASound;
		}
		
		public function set Channel(AChannel:SoundChannel):void
		{
			if(FCurrentVisualization)
				FCurrentVisualization.Channel = AChannel;
		}	
		
		public function set Visualization(AIndex:int):void
		{			
			for(var i in FVisualizations)
				FVisualizations[i].visible = false;
			
			if(AIndex > -1)
			{
				FCurrentVisualization = FVisualizations[AIndex];
				FCurrentVisualization.visible = true;
			}
		}
	}	
}
